/**

 @Name：用户管理
 @Author：xuqipeter
 @Site：http://blog.xuziyu.cn/
    
 */

layui.define(['table', 'layer', 'm-utils', 'form', 'admin', 'view'], function (exports) {
  var $ = layui.$,
    table = layui.table,
    layer = layui.layer,
    admin = layui.admin,
    view = layui.view,
    form = layui.form,
    setter = layui.setter,
    baseUrl = setter.baseUrl,
    ossUrl = setter.ossUrl,
    utils = layui['m-utils'];
  var OrderModel = {
    layerFull: null,
    tableList: null,
    /* 获取订单列表 */
    _tableList() {
      return table.render({
        elem: '#tableList',
        url: `${baseUrl}admin/order/getOrderList`,
        toolbar: '#theadToolbar',
        parseData: function (res) {
          return utils.setParseData({
            res,
            data: 'orderList'
          })
        },
        cols: [
          [{
              field: 'order_sn',
              title: '订单号',
              width: 185
            },
            {
              field: 'type',
              title: '类型',
              templet: function (d) {
                let text = '成品'
                if (d.type === 'parts') {
                  text = '配件'
                }
                return text
              }
            },
            {
              field: 'count',
              title: '数量',
            },
            {
              field: 'amount',
              title: '总金额（元）'
            },
            {
              field: 'number',
              title: '用户编号'
            },
            {
              field: 'receiver_name',
              title: '收货人姓名'
            },
            {
              field: 'receiver_area_name',
              title: '收货人省市区',
              width: 200
            },
            {
              field: 'receiver_address',
              title: '收货人地址',
              width: 200
            },
            {
              field: 'receiver_mobile',
              title: '收货人手机'
            },
            {
              field: 'created_time',
              title: '下单时间',
              width: 180,
              templet: function (d) {
                if (!d.created_time || d.created_time == 0) return ''
                return utils.formatTime(d.created_time)
              }
            },
            {
              field: 'express_company',
              title: '快递公司',
            },
            {
              field: 'express_number',
              title: '快递单号',
            },
            {
              field: 'delivered_time',
              title: '发货时间',
              width: 180,
              templet: function (d) {
                if (!d.created_time || d.delivered_time == 0) return ''
                return utils.formatTime(d.created_time)
              }
            },
            {
              field: 'status',
              title: '订单状态',
              width: 90,
              fixed: 'right',
              align: 'center',
              templet: function (d) {
                let text = ''
                switch (d.status) {
                  case 'created':
                    text = '<span class="layui-badge">待付款</span>'
                    break;
                  case 'paid':
                    text = '<span class="layui-badge layui-bg-green">待发货</span>'
                    break;
                  case 'delivered':
                    text = '<span class="layui-badge layui-bg-green">已发货</span>'
                    break;
                  case 'received':
                    text = '<span class="layui-badge layui-bg-gray">已收货</span>'
                    break;
                }
                return text
              }
            },
            {
              title: '操作',
              fixed: 'right',
              width: 90,
              align: 'center',
              toolbar: '#trToolbar'
            }
          ]
        ],
        done: function (d) {
          console.log(d)
          if (d[setter.response.statusName] == setter.response.statusCode.logout) {
            view.exit()
          }
        }
      })
    },
    /* 创建订单 */
    _createOrder({
      fieldData,
      callback
    }) {
      let {
        user_id,
        area_name,
        address,
        mobile,
        amount,
        count,
        receiver_name,
        type
      } = fieldData
      let that = this;
      admin.req({
        url: `${baseUrl}admin/order/createOrder`,
        method: 'post',
        data: {
          user_id,
          area_name,
          address,
          mobile,
          amount,
          count,
          receiver_name,
          type
        },
        dataType: 'json',
        done: function (res) {
          utils._doneSuccess({
            res,
            layerFull: that.layerFull,
            layTable: that.tableList
          })
          callback && callback(res)
        }
      })
    },
    /* 设置订单发货 */
    _deliverOrder({
      fieldData,
      callback
    }) {
      let {
        order_id,
        express_company,
        express_number,

      } = fieldData
      let that = this
      admin.req({
        url: `${baseUrl}admin/order/deliverOrder`,
        method: 'post',
        data: {
          order_id,
          express_company,
          express_number,
          callback
        },
        dataType: 'json',
        done: function (res) {
          utils._doneSuccess({
            res,
            layerFull: that.layerFull,
            layTable: that.tableList
          })
          callback && callback(res)
        }
      })
    },
    _add() {

    },
    _edit() {

    },
    _del() {

    }
  }

  exports('OrderModel', OrderModel)
})